package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.mapper.OrderDetailMapper;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.CartService;
import com.itheima.reggie.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private CartService cartService;
    @Autowired
     AddressMapper addressMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public void save(Order order) {
        //1.获取用户信息
        User user = UserHolder.get();
        //获取购物车信息
        List<Cart> cartList = cartService.cartList();
        if (CollectionUtil.isEmpty(cartList)) {
            throw new CustomException("购物车为空不能下订单");
        }
        //获取地址信息
        Address address = addressMapper.selectById(order.getAddressId());
        if (address==null) {
            throw new CustomException("收货地址为空不能下订单");
        }
        //4.封装订单明细并保存
        //使用雪花算法生成id
        long orderId = IdWorker.getId();
        //4.2订单总额
        BigDecimal amount = new BigDecimal(0);
//遍历购物车,封装定单信息
        for (Cart cart : cartList) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(cart.getName());//购物车菜品名称
            orderDetail.setOrderId(orderId);//订单id
            orderDetail.setDishId(cart.getDishId());//购物车菜品id
            orderDetail.setSetmealId(cart.getSetmealId());//购物车套餐id
            orderDetail.setDishFlavor(cart.getDishFlavor());//购物车口味
            orderDetail.setNumber(cart.getNumber());//购物车数量
            orderDetail.setAmount(cart.getAmount());//购物车金额
            orderDetail.setImage(cart.getImage());//购物车图片
            //订单总额金额累加: amount = amount + (cart.getAmount() * cart.getNumber());
            amount = amount.add(cart.getAmount().
                    multiply(new BigDecimal(cart.getNumber())));
            //保存订单明细
            orderDetailMapper.insert(orderDetail);
        }
        //5.封装订单并保存
order.setId(orderId);//订单id
        order.setNumber(String.valueOf(orderId));//订单
        order.setStatus(1);//代付款
        order.setUserId(user.getId());//用户id
        order.setOrderTime(new Date());//下单时间
        order.setCheckoutTime(new Date());//结账时间
        order.setAmount(amount);//订单金额
        order.setUserName(user.getName());//用户名称
        order.setPhone(address.getPhone());//收货人手机号\
        order.setAddress(address.getDetail());//收货人地址
        order.setConsignee(address.getConsignee());//收货人名称
        //保存订单
        orderMapper.insert(order);
        //清空购物车
        cartService.cartClean();

    }
    //后台订单明细查询
    @Override
    public Page<Order> findByPage2(Long number, Date beginTime, Date endTime, Integer pageNum, Integer pageSize) {
        //设置分页条件
        Page<Order> page = new Page<>(pageNum, pageSize);
        //2.设置条件
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(number != null, Order::getId, number);
        wrapper.eq(beginTime != null, Order::getOrderTime, beginTime);
        wrapper.eq(endTime != null, Order::getOrderTime, endTime);
        //设置排序
        wrapper.orderByDesc(Order::getOrderTime);
        //执行查询得到订单信息
        page= orderMapper.selectPage(page, wrapper);
        //遍历订单列表,得到单个订单
        List<Order> orderList = page.getRecords();
        if (CollectionUtil.isNotEmpty(orderList)) {
            //根据订单id从详情表获取详情信息
            for (Order order : orderList) {
                LambdaQueryWrapper<OrderDetail> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(OrderDetail::getOrderId, order.getId());
                List<OrderDetail> orderDetails = orderDetailMapper.selectList(wrapper1);
                order.setOrderDetails(orderDetails);
            }
        }//返回
        return page;

    }
//H5历史订单查询
    @Override
    public Page<Order> findByPage(Integer pageNum, Integer pageSize) {
        //设置分页条件
        Page<Order> page = new Page<>(pageNum, pageSize);
        //2.设置条件
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Order::getUserId, UserHolder.get().getId());
        //设置排序
        wrapper.orderByDesc(Order::getOrderTime);
        //执行查询,得到订单信息
        page = orderMapper.selectPage(page, wrapper);
        //5.遍历订单列表,得到单个订单
        List<Order> orderList = page.getRecords();
        if (CollectionUtil.isNotEmpty(orderList)) {
            for (Order order : orderList) {
                //根据订单id从详情表获取详情信息
                LambdaQueryWrapper<OrderDetail> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(OrderDetail::getOrderId, order.getId());
                List<OrderDetail> orderDetails = orderDetailMapper.selectList(wrapper1);
                order.setOrderDetails(orderDetails);

            }
        }
        //返回
        return page;


    }
}
